﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using DevExpress.XtraNavBar;
using DevExpress.XtraTab;

namespace Future.FormBase
{
    public enum NavigatorStyle
    {
        BarItem,
        BarContainer
    }

    /// <summary>
    /// 导航面板生成器基类
    /// </summary>
    public class NavigatorBase : INavigatorStyle
    {
        /// <summary>
        /// 程序主窗体的TabControl,用于放置模块主窗体
        /// </summary>
        protected ModuleManager _ModuleManager = null;

        /// <summary>
        /// 导航面板生成器基类
        /// </summary>
        public NavigatorBase(ModuleManager moduleManager)
        {
            _ModuleManager = moduleManager;
        }
        /// <summary>
        /// 实现接口,创建NavBar控件
        /// </summary>
        /// <param name="mainMenu"></param>
        /// <param name="navBar"></param>
        public virtual void CreateNavigator(MenuStrip mainMenu, NavBarControl navBar)
        {
            try
            {
                navBar.BeginUpdate();
                navBar.Groups.Clear();
                navBar.Items.Clear();
                foreach (XtraTabPage page in _ModuleManager.TabControlModules.TabPages)
                {
                    //取主窗体对应的顶级菜单
                    ToolStripMenuItem moduleMenu = MenuStripUtils.FindMenuItemByText(mainMenu, page.Text);
                    if (moduleMenu != null && moduleMenu.Enabled)//创建该模块的NavBarButton.
                    {
                        this.CreateNavBarButton(navBar, moduleMenu, page.Text);
                    }
                }
                if (navBar.Groups.Count == 0)
                    navBar.Groups.Add(new NavBarGroup(""));

                navBar.EndUpdate();
            }
            catch (Exception ex)
            {
                Common.Msg.ShowException(ex);
            }
        }
        /// <summary>
        /// 模板方法
        /// </summary>
        /// <param name="navBar"></param>
        /// <param name="moduleMenu"></param>
        /// <param name="moduleDisaplayName"></param>
        protected virtual void CreateNavBarButton(NavBarControl navBar, ToolStripMenuItem moduleMenu, string moduleDisaplayName)
        {
        }

        /// <summary>
        /// 当点击导航条分组按钮时触发该事件
        /// </summary>
        protected void OnActiveGroupChanged(object sender, DevExpress.XtraNavBar.NavBarGroupEventArgs e)
        {
            try
            {
                string moduleName = e.Group.Caption.ToString();
                _ModuleManager.ActiveModule(moduleName);
            }
            catch (Exception ex) { Common.Msg.ShowException(ex); }
        }
    }

    /// <summary>
    /// 生成项目列表样式的导航面板
    /// </summary>
    public class NavigatorBarList : NavigatorBase
    {
        /// <summary>
        /// 生成项目列表样式的导航面板
        /// </summary>
        public NavigatorBarList(ModuleManager moduleManager)
            : base(moduleManager)
        {
        }

        /// <summary>
        /// 创建导航条分组按钮 Navigator NavBarGroup
        /// </summary>
        /// <param name="navBar">NavBarControl对象</param>
        /// <param name="moduleMenu">模块主菜单</param>
        /// <param name="moduleDisplayName">模块名称</param>
        protected override void CreateNavBarButton(NavBarControl navBar, ToolStripMenuItem moduleMenu, string moduleDisaplayName)
        {
            navBar.ActiveGroupChanged += new NavBarGroupEventHandler(this.OnActiveGroupChanged);
            NavBarGroup group = CreateNavBarGroup(navBar, moduleDisaplayName);//根据模块名称创建分组按钮

            foreach (ToolStripItem item in moduleMenu.DropDownItems)
            {
                if (item is ToolStripSeparator) continue;
                if (MenuStripUtils.IsSubMenuOwner(item))
                {
                    CreateLinkByMenu(navBar, group, item, 0, true);
                    CreateLinkSubItem(navBar, group, item);
                }
                else
                {
                    CreateLinkByMenu(navBar, group, item, 0, false);
                }
            }
        }
        private void CreateLinkSubItem(NavBarControl navBar, NavBarGroup group, ToolStripItem menuItem)
        {
            if (menuItem is ToolStripMenuItem)
            {
                ToolStripMenuItem item = menuItem as ToolStripMenuItem;
                foreach (ToolStripItem subItem in item.DropDownItems)
                {
                    CreateLinkByMenu(navBar, group, subItem, -1, true);
                }
            }
        }

        /// <summary>
        /// 创建导航
        /// </summary>
        /// <param name="navBar"></param>
        /// <param name="group"></param>
        /// <param name="menuItem"></param>
        /// <param name="imageIndex"></param>
        /// <param name="isSubItem"></param>
        private void CreateLinkByMenu(NavBarControl navBar, NavBarGroup group, ToolStripItem menuItem, int imageIndex, bool isSubItem)
        {
            if (menuItem.Enabled)
            {
                NavBarItem item = CreateNavBarItem(navBar, menuItem, isSubItem);
                item.SmallImageIndex = imageIndex;
                group.ItemLinks.Add(item);
            }
        }
        /// <summary>
        /// 由菜单创建导航条控件按钮
        /// </summary>
        /// <param name="navBar"></param>
        /// <param name="menuItem"></param>
        /// <param name="isSubItem"></param>
        /// <returns></returns>
        private NavBarItem CreateNavBarItem(NavBarControl navBar, ToolStripItem item, bool isSubItem)
        {
            NavBarItem newItem = new NavBarItem();
            if (isSubItem)
                newItem.Caption = "  ● " + item.Text;
            else
                newItem.Caption = item.Text;
            newItem.Tag = item;//绑定一个菜单项,newItem.Click事件会使用這個菜单项
            newItem.LinkClicked += new NavBarLinkEventHandler(this.OnNavBarLinkClick);
            navBar.Items.Add(newItem);
            return newItem;
        }

        /// <summary>
        /// 导航条控件分组按钮
        /// </summary>
        /// <param name="navBar"></param>
        /// <param name="caption"></param>
        /// <returns></returns>
        private NavBarGroup CreateNavBarGroup(NavBarControl navBar, string caption)
        {
            NavBarGroup group = navBar.Groups.Add();
            group.Caption = caption;
            group.LargeImageIndex = 0;
            if (group.Caption.Equals(string.Empty)) group.Caption = "Unknown Name";
            return group;
        }

        /// <summary>
        /// Navigator bar 导航条按鈕事件.
        /// </summary>
        private void OnNavBarLinkClick(object sender, NavBarLinkEventArgs e)
        {
            if (e.Link.Item.Tag is ToolStripItem)
            {
                ToolStripItem item = (ToolStripItem)e.Link.Item.Tag;
                if (null != item)
                    item.PerformClick();

            }
        }
    }

    public class NavigatorTreeView : NavigatorBase
    {
        /// <summary>
        /// 生成树状样式的导航面板
        /// </summary>
        public NavigatorTreeView(ModuleManager moduleManager)
            : base(moduleManager)
        { }

        private void CreateGroupTreeView(NavBarGroup group, ToolStripMenuItem menuItem)
        {
            TreeView tv = new TreeView();
            tv.HotTracking = true;
            //tv.ImageList = ilModuleIcon;
            tv.ImageList = group.SmallImages as ImageList;
            tv.SelectedImageIndex = 1;
            tv.ShowRootLines = false;
            tv.Dock = DockStyle.Fill;
            TreeNode[] tns = new TreeNode[] { new TreeNode(menuItem.Text) };
            tv.NodeMouseClick += new TreeNodeMouseClickEventHandler(tv_NodeMouseClick);

            DevExpress.XtraNavBar.NavBarGroupControlContainer container = new DevExpress.XtraNavBar.NavBarGroupControlContainer();
            group.GroupStyle = NavBarGroupStyle.ControlContainer;
            container.Controls.Add(tv);
            group.ControlContainer = container;

            foreach (ToolStripItem item in menuItem.DropDownItems)
            {
                if (MenuStripUtils.IsSubMenuOwner(item))
                {
                    ToolStripMenuItem menuItems = item as ToolStripMenuItem;
                    TreeNode tn = new TreeNode(menuItems.Text);
                    //tn.Tag = menuItems;
                    tns[0].Nodes.AddRange(new TreeNode[] { tn });

                    CreateGroupTreeViewChild(tv, menuItems, tn);
                }
                else
                {
                    TreeNode itemNode = new TreeNode(item.Text);
                    itemNode.Tag = item;
                    tns[0].Nodes.Add(itemNode);
                }
            }
            tv.Nodes.AddRange(tns);
            tv.ExpandAll();
        }
        private void CreateGroupTreeViewChild(TreeView tv, ToolStripMenuItem parent, TreeNode parentNode)
        {
            foreach (ToolStripItem item in parent.DropDownItems)
            {
                if (item is ToolStripSeparator) continue;
                if (MenuStripUtils.IsSubMenuOwner(item))
                {
                    TreeNode tnChild = new TreeNode(item.Text);
                    tnChild.Tag = (ToolStripMenuItem)item;
                    parentNode.Nodes.AddRange(new TreeNode[] { tnChild });
                    CreateGroupTreeViewChild(tv, (ToolStripMenuItem)item, tnChild);
                }
                else
                {
                    TreeNode tnChild = new TreeNode(item.Text);
                    tnChild.Tag = item;
                    parentNode.Nodes.AddRange(new TreeNode[] { tnChild });
                }
            }
        }
        protected override void CreateNavBarButton(NavBarControl navBar, ToolStripMenuItem moduleMenu, string moduleDisaplayName)
        {
            navBar.ActiveGroupChanged += new NavBarGroupEventHandler(this.OnActiveGroupChanged);
            NavBarGroup group = CreateNavBarGroup(navBar, moduleDisaplayName);//根据模块名称创建分组按钮

            CreateGroupTreeView(group, moduleMenu);
        }
        private NavBarGroup CreateNavBarGroup(NavBarControl navBar, string caption)
        {
            NavBarGroup group = navBar.Groups.Add();//增加一個Button組.
            group.Caption = caption; //模塊的顯示名稱
            group.LargeImageIndex = 0;
            if (group.Caption.Equals(string.Empty)) group.Caption = "Unknown Name";
            return group;
        }
        private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag is ToolStripItem)
            {
                ToolStripItem item = (ToolStripItem)(e.Node.Tag);
                if (null != item)
                {
                    item.PerformClick(); //執行菜單事件.
                }
            }
        }
    }
}
